package com.csxy.xxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.xxm.entity.EduCourse;
import com.csxy.xxm.entity.EduCourseDescription;
import com.csxy.xxm.entity.vo.CourseFrontVo;
import com.csxy.xxm.entity.vo.CourseInfoForm;
import com.csxy.xxm.entity.vo.CoursePublishVo;
import com.csxy.xxm.entity.vo.CourseQuery;
import com.csxy.xxm.exception.MyException;
import com.csxy.xxm.mapper.EduCourseMapper;
import com.csxy.xxm.service.EduCourseService;
import com.csxy.xxm.service.EduVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterServiceImpl eduChapterService;

    //添加课程基本信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
//创建课程对象
        EduCourse eduCourse = new EduCourse();
        //封装课程基本信息
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int result = baseMapper.insert(eduCourse);
//获取课程id
        String cid = eduCourse.getId();
//创建课程描述对象
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        //封装课程描述
        eduCourseDescription.setId(cid);
        //插入课程描述
        return cid;

    }

    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //创建课程信息对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //获取课程信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //获取课程描述
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        //封装课程描述
        // 修复：添加空值检查
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        courseInfoForm.setDescription(description != null ? description.getDescription() : "");

        return courseInfoForm;
//        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());
//        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
//        修改课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        baseMapper.updateById(eduCourse);
//        修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);


    }

    @Override
    public boolean removeCourseById(String id) {
        //1.根据课程id删除小节
        eduVideoService.removeVideoByCourseId(id);
//        2.根据课程id删除章节部分
        eduChapterService.removeChapterByCourseId(id);

        //3.根据课程id删除课程描述
        eduCourseDescriptionService.removeById(id);
        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById( id);
        if (result>=1){
            return true;
        }else {
            throw new MyException(20001,"删除课程失败！");
        }
    }


    //删除课程
//    @Override
//    public boolean removeCourse(String id) {
//
//    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!title.isEmpty()) {
            queryWrapper.like("title", title);
        }
        if (!status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("gmt_modified");
        baseMapper.selectPage(pageParam, queryWrapper);

    }

    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Override
    public CoursePublishVo getCourseInfoById(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);

    }


    //课程列表（条件查询带分页）
    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        //拼接条件查询
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        eduCourseMapper.selectPage(pageCourse, wrapper);

        Map<String, Object> map = new HashMap<>();
        //总记录数
        long total = pageCourse.getTotal();
        //数据集合
        List<EduCourse> courseList = pageCourse.getRecords();
        //每条记录数
        long size = pageCourse.getSize();
        //当前页
        long current = pageCourse.getCurrent();
        //总页数
        long pages = pageCourse.getPages();
        //是否有上一页
        boolean hasPrevious = pageCourse.hasPrevious();
        //是否有下一页
        boolean hasNext = pageCourse.hasNext();

        map.put("total", total);
        map.put("list", courseList);
        map.put("size", size);
        map.put("current", current);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);
        return map;

    }

    @Override
    public CourseInfoForm getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    public List<EduCourse> selectHotCourse() {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

}